#!/usr/bin/env python
# coding=utf-8
# @FileName :response_model.py
# @Time     :2023/3/27 19:26
# @Author   :ZXC
# @Desc     :接口工具HttpClient响应模型
from typing import Union, List, Optional, Any

from pydantic import BaseModel, Field


class ResponseBaseModel(BaseModel):
    """
    HttpClient响应基本模型
    """
    status_code: int = Field(0, description='响应状态码')
    reason: str = Field('请求未成功', description='与响应状态码相对应的描述文本')
    ok: bool = Field(False, description='响应结果,小于等于400为True')
    elapsed: float = Field(0.0, description='响应时间（s）')
    url: str = Field('', description='响应url')
    encoding: Optional[str] = Field(None, description='响应编码-请求头获取')
    apparent_encoding: Optional[str] = Field(None, description='响应编码-分析响应内容获取')
    is_redirect: bool = Field(False, description='响应是否已重定向')
    is_permanent_redirect: bool = Field(False, description='响应是否已永久重定向')
    cookies: dict = Field({}, description='响应cookies')
    headers: dict = Field({}, description='响应头')
    content_type: str = Field('json', description='媒体类型')
    text: Optional[str] = Field(None, description='响应内容以指定编码格式编码后的字符串')
    content: Optional[bytes] = Field(None, description='二进制响应内容，主要为图片等')
    json_data: Optional[Union[str, dict, list]] = Field(None, alias='json', description='响应内容-json格式')
    iter_lines: Optional[Any] = Field(None, description='响应内容可迭代对象')
    iter_content: Optional[Any] = Field(None, description='二进制响应内容可迭代对象，主要为流式响应结果')
    raw: Optional[Any] = Field(None, description='原始响应体对象')
    request: Optional[Any] = Field(None, description='响应的请求对象')
    links: dict = Field({}, description='响应的标题链接')
    history: List = Field([], description='包含请求(URL)历史的响应对象列表')

    class Config:
        arbitrary_types_allowed = True


class ResponseStatModel(BaseModel):
    """
    HttpClient响应状态信息模型
    """
    start_time: str = Field('', description='开始时间')
    end_time: str = Field('', description='结束时间')


class HttpClientResponseModel(BaseModel):
    """
    HttpClient响应模型
    """
    response: ResponseBaseModel
    error: Optional[str] = Field('', description='错误信息')
    stat: ResponseStatModel
